package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Station;
import com.xy.service.StationService;
import com.xy.service.impl.StationServiceImpl;

/**
 * Servlet implementation class StationServlet
 */
@WebServlet("/stationServlet")
public class StationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	  
	  StationService service = new StationServiceImpl();
	  
	  if ("add".equals(request.getParameter("status"))) {
        
	    Station station = new Station();
	    station.setName(request.getParameter("name"));
	    station.setTel(request.getParameter("tel"));
	    station.setContactMan(request.getParameter("contactMan"));
	    station.setAddress(request.getParameter("address"));
	    station.setRemark(request.getParameter("remark"));
	    station.setCreateDate(new Date());
	    
	    if(service.add(station))
          request.getRequestDispatcher("/stationServlet?status=list&currentPage=0").forward(request,
              response);
        else {
          System.out.println("error");
          request.getRequestDispatcher("/pages/book/book003/station_add.jsp").forward(request,
              response);
        }
	    
      } else if ("list".equals(request.getParameter("status"))) {
        
        int currentPage = 0;
        if (request.getParameter("goalpage") == null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
        } else
          currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

        PageResults pageResults = service.listByPage(currentPage);
        
        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "list");
        request.setAttribute("list", null);

        request.getRequestDispatcher("/pages/book/book003/station_list.jsp").forward(request, response);
        
        
      } else if ("query".equals(request.getParameter("status"))) {
        
        int currentPage = 0;
        if (request.getParameter("goalpage") == null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
        } else
          currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
        
        String name = null;
        String list = null;
        
        if (request.getParameter("list") == null) {
          name = request.getParameter("name") == null ? "%" : request.getParameter("name");
          list = name;
        } else {
          list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          name = list;
        }

        PageResults pageResults = service.query(name, currentPage);

        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "query");
        request.setAttribute("list", list);

        request.getRequestDispatcher("/pages/book/book003/station_list.jsp").forward(request, response);
        
        
      } else if ("del".equals(request.getParameter("status"))) {
        
        int id = Integer.valueOf(request.getParameter("id"));
        String preStatus = request.getParameter("preStatus");
        int currentPage = 0;
        if (request.getParameter("currentPage") != null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage"));
        }
        
        Station station = new Station();
        station.setId(id);
        
        if("query".equals(preStatus)) {
          String list = null;
          if (request.getParameter("list") == null) {
            list = null;
          }else
            list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          
          request.setAttribute("list", list);
          if (service.delete(station)) {
              request.getRequestDispatcher("/stationServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            } else {
              System.out.println("error");
              request.getRequestDispatcher("/stationServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            }
        }else {
          if (service.delete(station)) {
              request.getRequestDispatcher("/stationServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            } else {
              System.out.println("error");
              request.getRequestDispatcher("/stationServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            }
        }
        
      } else if ("up".equals(request.getParameter("status"))) {
        
        int id = Integer.valueOf(request.getParameter("id"));
        Station station = service.get(id);
        station.setName(request.getParameter("name"));
        station.setTel(request.getParameter("tel"));
        station.setContactMan(request.getParameter("contactMan"));
        station.setAddress(request.getParameter("address"));
        station.setRemark(request.getParameter("remark"));
        
        if(service.update(station)) {
          request.getRequestDispatcher("/stationServlet?status=list&currentPage=0").forward(request,
              response);
        }else {
          System.out.println("error");
          request.getRequestDispatcher("/stationServlet?status=list&currentPage=0").forward(request,
              response);
        }
        
      } else if ("detail".equals(request.getParameter("status"))) {
        int id = Integer.valueOf(request.getParameter("id"));
        Station station = service.get(id);
        request.setAttribute("station", station);
        if("up".equals(request.getParameter("pre")))
          request.getRequestDispatcher("/pages/book/book003/station_modify.jsp").forward(request,
              response);
        else
          request.getRequestDispatcher("/pages/book/book003/station_detail.jsp").forward(request,
              response);
      }
	}

}
